package com.docjohnson.pamphlet.managers;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.provider.Settings;
import android.view.View;
import com.docjohnson.pamphlet.dialogs.Dialogs;
import com.docjohnson.pamphlet.listeners.PamphletClientListener;
import com.docjohnson.pamphlet.listeners.RequestInterface;
import com.docjohnson.pamphlet.server.Client;
import com.docjohnson.pamphlet.utils.Constants;
import com.docjohnson.pamphlet.utils.Utils;

//import com.docjohnson.pamphlet.dialogs.Dialogs;

public class PamphletClientController implements Constants {
    private final Activity activity;
    private Dialog dialog = null;

    public PamphletClientController(final Activity activity) {
        this.activity = activity;
    }

    void doRequest(final RequestInterface requestInterface, final boolean isRequired) {
        if (Utils.isOnline(activity)) {
//            Client.init(REST_SERVICE_URL, PamphletClient.MOBILE, REST_SERVICE_VERSION);
            requestInterface.doRequest();
        } else {
            if (isRequired) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showNoInternetConnectionCustomDialog(requestInterface);
                    }
                });
            } else {
                // TODO: cache request
            }
        }
    }

    public void getPamphlet(final Activity activity, final String pamphletId, final PamphletClientListener clientListener, final boolean isRequired) {
        doRequest(new RequestInterface() {
            @Override
            public void doRequest() {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            final String responseText = Client.getPamphlet(activity, pamphletId);
                            if (clientListener != null) {
                                clientListener.onSuccess(responseText);
                            }
                        } catch (final Exception e) {
//                            ExceptionHandler.getInstance().handleException(e);
                            if (clientListener != null) {
                                clientListener.onFail(e.getMessage());
                            }
                        }
                    }
                }.start();
            }

            @Override
            public void doCancel() {
                if (clientListener != null) {
                    clientListener.onCancel();
                }
            }
        }, isRequired);
    }

    public void getProduct(final Activity activity, final String productId, final PamphletClientListener clientListener, final boolean isRequired) {
        doRequest(new RequestInterface() {
            @Override
            public void doRequest() {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            final String responseText = Client.getProduct(activity, productId);
                            if (clientListener != null) {
                                clientListener.onSuccess(responseText);
                            }
                        } catch (final Exception e) {
//                            ExceptionHandler.getInstance().handleException(e);
                            if (clientListener != null) {
                                clientListener.onFail(e.getMessage());
                            }
                        }
                    }
                }.start();
            }

            @Override
            public void doCancel() {
                if (clientListener != null) {
                    clientListener.onCancel();
                }
            }
        }, isRequired);
    }

    public void needUpdateApplicationData(final Activity activity, final String appUpdateVersion, final PamphletClientListener clientListener, final boolean isRequired) {
        doRequest(new RequestInterface() {
            @Override
            public void doRequest() {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            final String responseText = Client.needUpdateApplicationData(activity, appUpdateVersion);
                            if (clientListener != null) {
                                clientListener.onSuccess(responseText);
                            }
                        } catch (final Exception e) {
//                            ExceptionHandler.getInstance().handleException(e);
                            if (clientListener != null) {
                                clientListener.onFail(e.getMessage());
                            }
                        }
                    }
                }.start();
            }

            @Override
            public void doCancel() {
                if (clientListener != null) {
                    clientListener.onCancel();
                }
            }
        }, isRequired);
    }

    void showNoInternetConnectionCustomDialog(final RequestInterface requestInterface) {
        Dialogs.closeProgressDialog(activity);
        if (dialog == null || !dialog.isShowing()) {
            dialog = Dialogs.showNoNetworkDialog(activity, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            doRequest(requestInterface, true);
                        }
                    }, new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
//				Dialogs.closeProgressDialog(activity);
                            activity.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                        }
                    }, new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            requestInterface.doCancel();
                        }
                    }
            );
        }
    }
}
